package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.QueryParam;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {

    @Autowired
    private ClazzService clazzService;
    /*
    * 条件分页查询
    * 班级信息*/
    @GetMapping
    public Result page(QueryParam queryParam){
        log.info("请求查询参数：QueryParam:{}", queryParam);
        PageResult pageResult = clazzService.page(queryParam);
        return Result.success(pageResult);
    }
    /*
    * 根据ID查询班级信息
    * 用于编辑时的数据回显
    * */
    @GetMapping("/{id}")
    public Result getClazzById(@PathVariable Integer id){
        log.info("请求id:{}", id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    /*
    * 查询所有班级信息
    * */
    @GetMapping("/list")
    public Result getAll(){
        log.info("查询所有班级信息");
        List<Clazz> clazzList = clazzService.getAll();
        return Result.success(clazzList);
    }

    /*
    * 新增班级*/
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz){
        log.info("新增班级，Clazz:{}", clazz);
        clazzService.addClazz(clazz);
        return Result.success();
    }

    /*
    * 修改班级信息
    * */
    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz){
        log.info("更新Clazz:{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    /*
    * 删除班级
    * */
    @DeleteMapping("/{id}")
    public Result deleteClazz(@PathVariable Integer id){
        log.info("要删除的班级id:{}", id);
        clazzService.delete(id);
        return Result.success();
    }
}
